package
{
	import BlackJack.BlackJackContext;
	import BlackJack.Model.VO.MultiClass;
	import BlackJack.Model.VO.Player;
	import BlackJack.Model.VO.TableData;
	import BlackJack.View.Debug;
	import BlackJack.View.Lobby;
	import BlackJack.View.Table;
	import com.adobe.protocols.dict.Dict;
	import com.xcopy.FileReader;
	import fl.controls.Button;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.MouseEvent;
	import flash.utils.Dictionary;

	[SWF(width = '1000', height = '666', backgroundColor = '#666666', frameRate = '24')]
	public class BlackJackMain extends Sprite
	{
		public var context:BlackJackContext;
		public var socket:FGS12Processer;
		public var lobby:Lobby           = new Lobby();
		public var table:Table           = new Table();
		private var reader:FileReader;
		public var client:Player         = new Player();
		public var debug:Debug;
		public static const LOBBY:String = 'LOBBY';
		public static const TABLE:String = 'TABLE';
		public static const DEBUG:String = 'DEBUG';
		private var _state:String        = '';
		public var tableType:int         = -1;
		private var track:Button;
		private var formDict:Dictionary  = new Dictionary();

		public function BlackJackMain()
		{
			socket = new FGS12Processer(this);
			reader = new FileReader();
			reader.listen(onFileLoaded, onFileLoading, onFileMessage);
			context = new BlackJackContext(this);
			debug = new Debug(this);
			debug.display = new BlackJackDebugger();
			track = new Button();
			track.setSize(40, 20);
			track.label = 'Debug';
		}

		public function start():void
		{
			reader.swallow('BlackJack', 'BlackJack.swf');
			reader.read('Form', 'form.txt');
		}

		public function get state():String
		{
			return _state;
		}

		public function set state(value:String):void
		{
			if (_state == value)
			{
				return;
			}
			switch (_state)
			{
				case LOBBY:
				{
					lobby.visible = false;
					break;
				}
				case TABLE:
				{
					table.visible = false;
					break;
				}
			}
			_state = value;
			switch (value)
			{
				case LOBBY:
				{
					lobby.visible = true;
					break;
				}
				case TABLE:
				{
					table.Color = tableType;
					table.delayPoint(client.GPOINT);
					table.visible = true;
					break;
				}
			}
		}

		private function onFileLoaded(ID:String, content:String):void
		{
			switch (ID)
			{
				case 'BlackJack':
				{
					lobby.display = reader.spit('BlackJack', 'BlackJack_Lobby');
					lobby.visible = false;
					addChild(lobby);
					table.display = reader.spit('BlackJack', 'BlackJack_Game');
					table.visible = false;
					addChild(table);
					addChild(debug);
					addChild(track);
					track.addEventListener(MouseEvent.CLICK, openDebug);
					break;
				}
				case 'Form':
				{
					var array:Array = content.split('\r\n');
					var s:String;
					var formula:Array;
					var left:int;
					var right:String;
					var stack:Array;
					var mc:MultiClass;
					var len:uint;
					var i:int;
					var temp:String;
					var a:Array;
					for each (s in array)
					{
						if (s.length > 0)
						{
							mc = new MultiClass();
							formula = s.split('=');
							left = int(formula[0]);
							right = formula[1];
							stack = right.split('+');
							len = stack.length;
							for (i = 0; i < len; i++)
							{
								temp = stack[i];
								a = temp.split('*');
								mc.num[i] = uint(a[0]);
								mc.count[i] = uint(a[1]);
							}
							formDict[left] = mc;
						}
					}
					table.formDict = formDict;
					break;
				}
			}
		}

		private function openDebug(event:MouseEvent):void
		{
			debug.visible = true;
		}

		private function onFileLoading(ID:String, percent:Number):void
		{
			//debug.LOG(ID + '\t' + percent);
		}

		private function onFileMessage(ID:String, message:String):void
		{
			//trace(message);
			debug.LOG(ID + '\t' + message);
		}

		public function updateClient(value:Player):void
		{
			lobby.delayPoint(value.GPOINT);
			client = value;
		}

		public static function Trim(src:String):String
		{
			var result:String;
			result = src.replace(/ /g, '');
			result = result.replace(/\r\n/g, '\n');
			result = result.replace(/\n\n/g, '');
			return result;
		}

		public static function updateValue(mc:MovieClip, value:int):void
		{
			var sValue:String     = Math.abs(value).toString();
			var len:int           = sValue.length;
			var digits:Dictionary = new Dictionary(true);
			var commas:Dictionary = new Dictionary(true);
			var digitCount:int    = 0;
			var commaCount:int    = 0;
			var obj:Object;
			for each (obj in mc)
			{
				if (obj is MovieClip)
				{
					var name:String = MovieClip(obj).name;
					var index:int   = int(name.substring(5, name.length));
					name = name.substr(0, 5);
					switch (name)
					{
						case 'Digit':
						{
							digits[index] = obj;
							digitCount++;
							break;
						}
						case 'Comma':
						{
							commas[index] = obj;
							commaCount++;
							break;
						}
					}
				}
			}
			var i:int;
			for (i = 0; i < digitCount; i++)
			{
				if (i < len)
				{
					MovieClip(digits[i]).gotoAndStop('d' + sValue.substr(len - i - 1, 1));
				}
				else if (i == len && value < 0)
				{
					MovieClip(digits[i]).gotoAndStop('Minus');
				}
				else
				{
					MovieClip(digits[i]).gotoAndStop('empty');
				}
			}
			var show:int = (len - 1) / 3;
			for (i = 0; i < commaCount; i++)
			{
				if (i < show)
				{
					MovieClip(commas[i]).gotoAndStop('show');
				}
				else
				{
					MovieClip(commas[i]).gotoAndStop('hide');
				}
			}
		}
	}
}
